from django.conf import settings
from django.db import models

from account.models import User
from common.models import Common


class ShopInfo(Common):
    """店铺设置"""
    master = models.ForeignKey(User, verbose_name=u'所属用户', related_name='shop_master',
                               null=True, help_text=u'店铺所属用户', on_delete=models.SET_NULL)
    user = models.OneToOneField(User, verbose_name=u'操作用户', related_name='shop_user',
                                null=True, blank=True, help_text=u'店铺操作用户', on_delete=models.SET_NULL)
    title = models.CharField(u'店铺名称', max_length=255, default='')
    logo = models.CharField(u'logo图片', max_length=255, default='', null=True, blank=True)
    images = models.CharField(u'图片组', max_length=1025, default='')
    phone = models.CharField(u'联系电话', max_length=128, default='')
    address = models.CharField(u'地址', max_length=256, default='')
    province_address = models.CharField(u'省地址', max_length=256, default='', blank=True)
    city_address = models.CharField(u'市地址', max_length=256, default='', blank=True)
    country_address = models.CharField(u'区/县地址', max_length=256, default='', blank=True)
    desc = models.TextField(u'富文本介绍', default='')

    def get_full_addr(self):
        return self.province_address + self.city_address + self.city_address + self.address

    def get_logo_url(self):
        return settings.QINIU_DOMAIN + self.logo

    def get_images(self):
        return [
            {'name': item, 'url': settings.QINIU_DOMAIN + item}
            for item in self.images.split(',')
        ]

    class Meta:
        db_table = 'shop_info'
        verbose_name = '店铺信息'
        ordering = ['-create_time']
